/**
 * FlashLoader JavaScript utility.
 * 
 * @version 1.0
 */

var FlashLoader = FlashLoader || {};
FlashLoader = (function(){
	
	/**
	 * Reference to embedded FlashLoader swf
	 */
	var _swf;
	
	/**
	 * FlashLoader settings.
	 */
	var _settings = {
		debug: false, // output debug info
		debugMode: 'console', // debug output method ('console' or 'alert')
		onReady: null // 
	};
	
	/**
	 * Returns FlashLoader swf dom element.
	 * @param {String} name Name of the embedded flash object
	 */
	var _getFlashMovie = function(name)
	{
		var isIE = navigator.appName.indexOf("Microsoft") != -1;   
		return (isIE) ? window[name] : document[name];
	}
	
	/**
	 * Loads a file and supplies optional callback with
	 * loaded data.
	 * 
	 * @param {String} url URL of file to load
	 * @param {String} callback Name of callback method
	 */
	var _load = function(url, callback)
	{
		_debug('Loading url: '+url+'.');
		try {
			_swf.load(url, callback);
		}
		catch(error)
		{
			_debug('Could not load url: '+url+'. '+error.message);
		}	
	}
	
	
	/**
	 * Adds a file and optional callback to the FlashLoader queue.
	 * 
	 * @param {String} url URL of file to load
	 * @param {String} callback Name of callback method
	 */
	var _addToQueue = function(url, callback)
	{
		_debug('Adding url: '+url+' to queue.');
		
		try {
			_swf.addToQueue(url, callback);
		}
		catch(error)
		{
			_debug('Could not add url: '+url+' to queue.');
		}
	}
	
	/**
	 * Loads all items that have been added to the queue.
	 * 
	 * @param {String} callback Name of callback method
	 * @param {String} maxConcurrent Number of simulaneous loads
	 */
	var _loadQueue = function(callback, maxConcurrent)
	{
		_debug('Loading queue.');
		try {
			_swf.loadQueue(callback, maxConcurrent);
		}
		catch(error)
		{
			_debug('Could not load queue.');
		}
	}
	
	/**
	 * Called from the FlashLoader swf when it has loaded
	 * and is ready to use.
	 */
	var _ready = function()
	{
		_debug('Ready.');
		_swf = _getFlashMovie('FlashLoader');
		if(_settings.onReady instanceof Function)
		{
			_settings.onReady();
		}
	}
	
	/**
	 * Output FlashLoader debug information
	 * @param {String} msg
	 */
	var _debug = function(msg)
	{
		msg = 'FlashLoader: '+msg;
		if(_settings.debug)
		{
			switch(_settings.debugMode)
			{
				
				case 'alert':
					alert(msg);
					break;
				case 'console':
					console.log(msg);
					break;
				default:
					alert(msg);
					break;
			}
		}
	}
	
	/**
	 * Output error messages from the FlashLoader swf if debugging is enabled.
	 * @param {String} msg
	 */
	var _error = function(msg)
	{
		_debug(msg);
	}
	
	/**
	 * Initialize FlashLoader utility.
	 * @param {Object} settings
	 */
	var _init = function(settings)
	{
		for(attr in settings)
		{
			_settings[attr] = settings[attr];
		}
		
		_debug('Initialized.');
	}
	
	// expose public properties and methods
	return {
		init: _init,
		load: _load,
		addToQueue: _addToQueue,
		loadQueue: _loadQueue,
		error: _error,
		swf: _swf,
		settings: _settings,
		ready: _ready
	}
}());